/* Copyright 2010 by Matt Van Der Westhuizen */

package org.chaoticengine.ast;

import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import org.chaoticengine.ast.model.AirShip;
import org.chaoticengine.ast.model.World;
import org.chaoticengine.ast.model.WorldMap;
import org.chaoticengine.ast.pattern.Model;
import org.chaoticengine.ast.pattern.ViewController;
import org.chaoticengine.ast.view.WorldMapView;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class GlobalData extends Model {
    protected static GlobalData instance = null;

    /** The game configuration is read from a .properties file. */
    protected Properties config = null;

    /** Single random number generator (god :P) instance used by the entire game. */
    protected Random rng = null;

    /** The world. */
    protected World world = null;

    /** The currently selected View of a Model object is stored here. */
    protected ViewController<? extends Model> selected = null;

    /** The view of the world map. */
    protected WorldMapView mapView = null;

    /** The ship of the player. */
    protected AirShip playerShip = null;

    private GlobalData() {
        super();

        // Load configuration
        config = new Properties();
        try {
            config.load(new FileReader("config.properties"));
        } catch (IOException ioEx) {
            System.out.println("Error: Failed to load configuration file 'config.properties'");
            System.exit(-1);
        }

        // Seed random number generator
        rng = new Random(new Date().getTime());
    }

    public static GlobalData getInstance() {
        if (instance == null) {
            instance = new GlobalData();
        }

        return(instance);
    }

    /**
     * Returns the configuration item identified by <b>key</b>.
     * @param key The key for the configuration item.
     * @return The configuration value, or null if no such key is found.
     */
    public String getConfig(String key) {
        return((String) config.getProperty(key));
    }

    public Random getRNG() {
        return(rng);
    }

    public World getWorld() {
        return world;
    }

    public void setWorld(World world) {
        this.world = world;
        refreshObservers();
    }

    public WorldMap getMap() {
        return(this.world.getMap());
    }

    public void setMap(WorldMap map) {
        this.world.setMap(map);
        refreshObservers();
    }

    public ViewController<? extends Model> getSelected() {
        return(selected);
    }

    public void setSelected(ViewController<? extends Model> t) {
        if (selected != null) {
            selected.setSelected(false);
        }

        if (t != null) {
            t.setSelected(true);
            selected = t;
        } else {
            selected = null;
        }

        refreshObservers();
    }

    public WorldMapView getMapView() {
        return mapView;
    }

    public void setMapView(WorldMapView mapView) {
        this.mapView = mapView;
        refreshObservers();
    }

    public AirShip getPlayerShip() {
        return playerShip;
    }

    public void setPlayerShip(AirShip playerShip) {
        this.playerShip = playerShip;
        refreshObservers();
    }

    
}
